package list.control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * Created by ZhipengLi.
 */
public abstract class Guest extends MysqlLink {
    private static final String TABLE = "guest";

    public static boolean makeAuth(String session_id, String user) {
        return checkAuth(session_id, user) || insert(TABLE, toArray("auth", "time", "user"), toArray(session_id, (new Date()).getTime() + "", user));
    }

    private static boolean checkAuth(String session_id, String user) {
        return exist(TABLE, "session_id", equalsAnd(toArray("session_id", "user"), toArray(session_id, user)));
    }

    public static String getUser(String auth) {
        ResultSet resultSet = select(TABLE, "user", equal("auth", auth));
        String user = null;
        if (null != resultSet) {
            try {
                if (resultSet.next()) {
                    user = resultSet.getString("user");
                    resultSet.close();
                }
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return user;
    }
}
